package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Course;
import com.qk.management.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 课程管理控制器
 * @Author:Mr.m
 * @Date: 2025-10-09 11:10
 * @Description: TODO
 */
@Slf4j
@RestController
@RequestMapping("/courses")
public class CourseController {
    @Autowired
    private CourseService courseService;

    //根据学科查询课程
    @GetMapping("/subject/{subject}")
    public Result queryCourseBySubject(@PathVariable Integer subject){
        log.info("查询课程,参数:subject:{}",subject);
        List<Course> courses = courseService.queryCourseBySubject(subject);
        return Result.success(courses);
    }

    //删除课程
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        log.info("删除课程id:{}",id);
        courseService.deleteById(id);
        return Result.success();
    }

    //修改课程
    @PutMapping
    public Result updateCourse(@RequestBody Course course){
        log.info("修改课程:{}",course);
        courseService.updateCourse(course);
        return Result.success();
    }

    //根据id查询（回显数据）
    @GetMapping("/{id}")
    public Result queryCourseById(@PathVariable("id") Integer id) {
        log.info("查询课程id:{}",id);
        Course course = courseService.queryCourseById(id);
        return Result.success(course);
    }

    //添加课程
    @PostMapping
    public Result addCourse(@RequestBody Course course) {
        log.info("添加课程:{}",course);
        courseService.addCourse(course);
        return Result.success();
    }

    //查询所有课程
    @GetMapping("/list")
    public Result list() {
        List<Course> courses = courseService.list();
        return Result.success(courses);
    }

    //分页查询所有课程
    @GetMapping
    public Result queryCourseByPage(
            @RequestParam(name = "name", required = false) String name,
            @RequestParam(name = "subject", required = false) Integer subject,
            @RequestParam(name = "target", required = false) Integer target,
            @RequestParam(name = "page", defaultValue = "1") Integer page,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        log.info("查询课程,参数:name:{},subject:{},target:{},page:{},pageSize:{}", name, subject, target, page, pageSize);
        PageResult<Course> pageResult =courseService.page(name, subject, target, page, pageSize);
        return Result.success(pageResult);
            }
}
